package com.lib.okhttp.interceptor;

import com.lib.utils.XLog;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 重试拦截器
 */
public class RetryInterceptor implements Interceptor {
    //最大重试次数
    private final int maxRetryCount;

    public RetryInterceptor(int maxRetryCount) {
        this.maxRetryCount = maxRetryCount;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request oldRequest = chain.request();
        Response response = chain.proceed(oldRequest);

        int retryCount = 0;
        while (!response.isSuccessful() && retryCount < maxRetryCount) {
            XLog.i(retryCount + "/" + maxRetryCount);
            response = chain.proceed(oldRequest);

            retryCount++;
        }
        return response;
    }
}